using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using THANHTUNGO.Portal.Components.UI;

namespace THANHTUNGO.Portal.Modules.SECURITY.Controls
{
    public partial class EditUser : AddEditItemPage
    {
        protected override void OnInit(EventArgs e)
        {
            ButtonUpdate = this.btnUpdate;
            ButtonCancel = this.btnCancel;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    THANHTUNGO.Portal.Components.Security.User user = THANHTUNGO.Portal.Components.Security.User.Load(this.ItemID);
                    txtName.Text = user.Name;
                    rbGenderMale.Checked = user.Gender;
                    rbGenderFemale.Checked = !user.Gender;
                    txtEmail.Text = user.Email;
                    txtAddress.Text = user.Address;
                    txtPhone.Text = user.Phone;
                    // Hidden fields.
                    txtPassword.Value = user.Password;
                    txtJoinedDate.Value = user.JoinedTime.ToString();
                }
            }
        }

        protected override void OnUpdate(EventArgs e) 	 
        {
            base.OnUpdate(e);

            if (Page.IsValid)
            {
                THANHTUNGO.Portal.Components.Security.User user = new THANHTUNGO.Portal.Components.Security.User();
                user.Name = txtName.Text;
                user.Gender = rbGenderMale.Checked;
                user.Email = txtEmail.Text;
                user.Address = txtAddress.Text;
                user.Phone = txtPhone.Text;
                

                // Hidden fields.
                user.Password = txtPassword.Value;
                

                if (this.ItemID == 0)
                {
                    user.JoinedTime = DateTime.Now;
                    user.Insert();
                }
                else
                {
                    user.ID = this.ItemID;
                    user.JoinedTime = DateTime.Parse(txtJoinedDate.Value);
                    user.Update();
                }
                this.ReturnToReferrerPage();
            }
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCountry.SelectedValue.Equals("VN"))
            {
                ddlCity.Visible = true;
                txtCity.Visible = false;
            }
            else
            {
                ddlCity.Visible = false;
                txtCity.Visible = true;
            }
        }
    }
}
